package com.example.users.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.users.domain.User;
import com.example.users.service.UserService;
import com.example.users.common.PageResponse;
import com.example.users.common.PageUtil;
import com.example.users.common.ApiResponse;
import com.example.users.common.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

@RestController
@RequestMapping("/api/users")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping
    public ApiResponse<PageResponse<User>> list(@RequestParam(defaultValue = "1") long page,
                                  @RequestParam(defaultValue = "10") long size,
                                  @RequestParam(required = false) String keyword,
                                  @RequestParam(required = false, defaultValue = "id") String sortBy,
                                  @RequestParam(required = false, defaultValue = "desc") String sortOrder) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            qw.lambda()
              .like(User::getUsername, keyword)
              .or().like(User::getEmail, keyword);
        }
        if ("username".equalsIgnoreCase(sortBy)) {
            qw.orderBy(true, "asc".equalsIgnoreCase(sortOrder), "username");
        } else if ("email".equalsIgnoreCase(sortBy)) {
            qw.orderBy(true, "asc".equalsIgnoreCase(sortOrder), "email");
        } else {
            qw.orderBy(true, "asc".equalsIgnoreCase(sortOrder), "id");
        }
        Page<User> result = userService.page(Page.of(page, size), qw);
        return ApiResponse.success(PageUtil.toResponse(result));
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> detail(@PathVariable Long id) {
        User user = userService.getById(id);
        return ResponseEntity.ok(user);
    }

    @PostMapping
    public ApiResponse<?> create(@Validated(com.example.users.validation.groups.Create.class) @RequestBody User user) {
        user.setId(null);
        boolean ok = userService.save(user);
        return ApiResponse.success(ok ? user : null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @Validated(com.example.users.validation.groups.Update.class) @RequestBody User user) {
        user.setId(id);
        boolean ok = userService.updateById(user);
        return ApiResponse.success(ok ? user : null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Boolean> delete(@PathVariable Long id) {
        boolean ok = userService.removeById(id);
        return ApiResponse.success(ok);
    }
}


